/**
 * 武器协议
 */
class WeaponPrase {
    private static _instacne:WeaponPrase = null;

    constructor()
    {

    }

    public static instance()
    {
        if(!this._instacne)
        {
            this._instacne = new WeaponPrase();
        }
        return this._instacne;
    }

    public getWeaponInfo(call:Function, scope)
    {
         let data = {
            version:GameConst.version
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorweapon/get_weapon_info', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data && data.data)
            {
                RoleCache.instance().curHadWeapon = data.data.have_list;
                RoleCache.instance().curExperienceWeapon = parseInt(data.data.experience_weapon);
                let curWeapon = data.data.current_weapon;
                let parma = {
                    shopList:data.data.shop_list,
                    video_num:data.data.video_num,
                    invite_num:data.data.invite_num
                }
                if(curWeapon != RoleCache.instance().curWeaponId)
                {
                    RoleCache.instance().curWeaponId = curWeapon;
                    GameUtil.dispatchEvent(EventConst.CHANGE_WEAPON);
                }
                if(call)
                {
                    call.call(scope, parma);
                }
            }
            else
            {
                MessageUtil.tips('网络错误');
            }
        }, this);
    }

    public buyWeapon(parma, call?:Function, scope?)
    {
        let data = {
            version:GameConst.version,
            id:parma.id
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorweapon/buy_weapon', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                RoleCache.instance().myMoney = data.data.coin;
                RoleCache.instance().myDiamond = data.data.diamond;
                RoleCache.instance().curHadWeapon.push(parma.id+'');
                if(call)
                {
                    call.call(scope);
                }
                let itemId = parma.itemId;
                if(itemId)
                {
                    let award = `a_${itemId}_1`;
                    GameUtil.openAwardUI(award,null,null);
                }
               
                MessageUtil.tips('购买成功！');
            }   
            else
            {
                MessageUtil.tips('购买失败');
            }
        }, this);
    }

    public useWeapon(parma, call?:Function, scope?)
    {
        let data = {
            version:GameConst.version,
            weapon_id:parma.weapon_id
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorweapon/change_weapon', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                if(call)
                {
                    call.call(scope);
                }
                RoleCache.instance().curWeaponId = data.data.weapon;
                GameUtil.dispatchEvent(EventConst.CHANGE_WEAPON);
                MessageUtil.tips('使用成功');
            }   
            else
            {
                MessageUtil.tips('网络错误');
            }
        }, this);
    }
}